<?php
namespace app\mobile\controller;
use think\Db;
use think\Controller;
use think\Session;
use think\Request;
use think\Cookie;
use app\common\func\allRequest;

class Index extends Controller
{
    public function index()
    {   
        ( empty(cookie::get('loginid')) && empty(session::get('user')) )? $uid = '' : (!empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid'));
        if(!empty($uid)){
             $res = Db::field('d.name')->table(['s75_data'=>'d','s75_orders'=>'o'])
                ->where('d.id=o.cid')->where('o.status=0')->where('o.uid='.$uid)->select();

            // 存session
            session::set('user.count',count($res));
        }
       
        // 热门推荐
        $hot = db('data')->field(['id','name','face','author','price','tag'])->order('hot desc')->limit(5)->select();
        // 最新上线
        $new = db('data')->field(['id','name','face','author','price','tag'])->order('ctime')->limit(5)->select();
        // 最受好评
        $score = db('data')->field(['id','name','face','author','price','tag'])->order('score desc')->limit(5)->select();
        // 经典推荐
        $search =  db('data')->field(['id','name','face','author','price','tag'])->order('search desc')->limit(5)->select();

        $this->assign('hot',$hot);
        $this->assign('new',$new);
        $this->assign('score',$score);
        $this->assign('search',$search);
        return view('index/index',[
            'title'=>'新芽阅读',
            'hot'=>$hot,
            'new'=>$new,
            'score'=>$score,
            'search'=>$search
        ]);
    }

    // 查看更多
    public function showMore(Request $request)
    {   
        $res = $request->param();
        $where = $res['where'];

        switch ($where) {
         case '1':// 新书
             $data = $this->showAll('ctime');
             $name = '最新上线';
             break;
         case '2':// 热搜
             $data = $this->showAll('search desc');
             $name = '经典推荐';
             break;
         case '3':// 热门
             $data = $this->showAll('hot desc');
             $name = '热门推荐';
             break;
         default:// 好评
             $data = $this->showAll('score desc');
             $name = '最受好评';
             break;
        }
        
        // 分页
         $page = $data->render();
         return view('index/showAll',[
            'title'=>'榜单',
            'name'=>$name,
            'data'=>$data,
            'page'=>$page
        ]);
    }

    // 遍历全部书籍(排序)
    public function showAll($where)
    {
        
         $data = Db::field('d.id, d.name, price, author, face, tag')
        ->table(['s75_data'=>'d','s75_category'=>'c'])
        ->where('c.id = d.cid')
        ->order($where)
        ->paginate(20);
        return $data;
    }



    // 搜索图书
    public function find(Request $request)
    {   
        $post = $request->post();
        $search = $post['search'];
        // 搜索结果
        $res = db('data')->field(['id','name','face','price','author','tag'])
            ->where('name','like','%'.$search.'%')->whereor('author','like','%'.$search.'%')->select();
        // 搜索量 +1
        foreach ($res as $k => $v) { $ids[] = $v['id']; }
        db('data')->where('id','in',$ids)->setInc('search');
        // 热门搜索
        $recom =  db('data')->field(['id','name','face','author','price','tag'])
                ->where('id','not in',$ids)->order('search desc')->limit(5)->select();
        // 其他购买
        $other =  db('data')->field(['id','name','face','author','price','tag'])
                ->where('id','not in',$ids)->order('sale desc')->limit(5)->select();
        return view('index/find',[
            'title'=>'搜索——'.$search,
            'count'=>count($res),
            'res'=>$res,
            'recom'=>$recom,
            'other'=>$other
        ]);
    }


    public function callapi()
    {
        $url = "http://www.s75.com/index/Index/api";
        $urlreq = new allRequest(); 
        $msg = $urlreq->curl_request($url);
        dump($msg);
        dump(json_decode($msg));
    }

    public function api()
    {
        $data = db('data')->field(['id','name','face','author','price'])->limit(6)->select();
        return json($data);
    }

}
